package org.nerve.okhttp3.cookie;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

import java.util.List;

/**
 * org.nerve.okhttp3.cookie
 * Created by zengxm on 2016/7/25.
 */
public class CookieManager implements CookieJar {

	protected ICookieStore cookieStore;

	public CookieManager(ICookieStore cookieStore){
		this.cookieStore=cookieStore;
	}

	@Override
	public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
		if(list==null || list.isEmpty()) return;

		list.forEach(c->cookieStore.add(httpUrl, c));
	}

	@Override
	public List<Cookie> loadForRequest(HttpUrl httpUrl) {
		List<Cookie> cookieList=cookieStore.get(httpUrl);
		return cookieList;
	}
}
